package com.ld.hpfs.web.controller;

import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.TimerTask;

import javax.xml.namespace.QName;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.ld.hpfs.common.printer.client.InvoiceStub;
import com.ld.hpfs.common.printer.client.Printer;
import com.ld.hpfs.common.printer.client.PrinterService;
import com.ld.hpfs.common.utils.Const;
import com.ld.hpfs.service.DealService;
@Controller
public class PrintedStub extends TimerTask {
	
	@Autowired
	private DealService dealService;
	
	
	private Printer port;
	private String clientIpPort ;
	private List<InvoiceStub> stubs;
	private PrinterService services = null;		
	private static final QName qName = new QName("http://service.printer.hps.com/", "PrinterService");
	private static final Logger logger = LoggerFactory.getLogger(PrintedStub.class);
	@Override
	public void run() {
		try{
			clientIpPort = Const.PROPERTIES.getProperty("clientIpPort");
			HashMap<String,Object> IpInfo =dealService.getClientIp();
			logger.debug("{}", "IpInfo:"+IpInfo);
			if(IpInfo!=null){
				String clientIp = (String)IpInfo.get("RETOME_ADDR_IP");//获得打印AI接口的ip
				Integer QUANTITY = (Integer)IpInfo.get("QUANTITY");//获得要打印的数量
				Integer COMPANY_ID = (Integer)IpInfo.get("COMPANY_ID");
				Integer ID = (Integer)IpInfo.get("ID");
				logger.debug("{}", "Ip:"+clientIp+"num:"+QUANTITY+"COMPANY_ID:"+COMPANY_ID);
//				String clientIp = "222.44.80.109";
//				Integer COMPANY_ID = 1;
//				Integer QUANTITY = 0;				
				HashMap<String,Object> Clientparam = new HashMap<String,Object>();
				Clientparam.put("clientIp", clientIp);
				Clientparam.put("ID", ID);
				if (clientIp != null && QUANTITY != 0) {
					//throw new RuntimeException("Codeing error, PrinterCtrl must init(ip) before use.");			
					try {

						services = new PrinterService(new URL("http://"+clientIp +":"+clientIpPort+ "/IPrinter?wsdl"), qName);
						port = services.getPrinterPort();	
						stubs = port.rtnPrintedStubs();	
						logger.debug("{}", "stubs:"+stubs);
						if(stubs.isEmpty()){//没有打印结果
							
						}else if(!stubs.isEmpty()){
							ArrayList<HashMap<String,Object>> list = new ArrayList<HashMap<String,Object>>();
							for (InvoiceStub invStub : stubs) {
								HashMap<String,Object> param = new HashMap<String,Object>();
								logger.debug("{}", "invStub:"+invStub);
								String HeadCode = invStub.getHeadCode();    //唯一标识
								String InvoiceCode = invStub.getInvoiceCode(); //发票代码
								if(InvoiceCode==null){
									InvoiceCode = "0";
								}
								String InvoiceNo = invStub.getInvoiceNo();   //发票号码	
								
								String date = invStub.getInvoiceDate(); //打印日期
								Date t = new SimpleDateFormat("yyyyMMddhhmmss").parse(date);
								date = new SimpleDateFormat("yyyyMMdd hh:mm:ss").format(t);
							
								String printer = invStub.getDrawer();      //打印人
								Short PrintStatus = invStub.getPrintStatus(); //打印状态  【0、成功打印； 1、打印失败】
								
								param.put("InvoiceCode", InvoiceCode);																	
								param.put("InvoiceNo", InvoiceNo);												
								param.put("HeadCode", HeadCode);
								param.put("PrintStatus", PrintStatus);	
								param.put("date", date);
								param.put("printer", printer);
								param.put("clientIp", clientIp);
								param.put("COMPANY_ID", COMPANY_ID);
								dealService.updateDoc_StatusTo1(param);
							}
							logger.debug("{}", "list:"+list);
							
							//把打印成功的结果集的状态改为"已开票"								
//							int num = stubs.size();
							HashMap<String,Object> param = new HashMap<String,Object>();
							param.put("clientIp", clientIp);							
							param.put("ID", ID);
							Integer count = dealService.getIPCount(param);
							int number= count-stubs.size();
							param.put("number", number);
							logger.debug("{}", "number:"+number);
							if(number>0){//说明还有未打印完的数据								
								dealService.editNum(param);//修改数据库中还要打印的条数
							}else{
								dealService.delectClientIp(param);//把打印成功的ip在表中删除
							}
						}
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
//						dealService.delectClientIp(Clientparam);
					}
				 
//					List<InvoiceStub> stubs=new ArrayList<InvoiceStub>();
//					InvoiceStub in = new InvoiceStub();
//					in.setInvoiceNo("00000098");
//					in.setInvoiceCode("");
//					in.setHeadCode("GTS_0000000027");
//					in.setDrawer("superadmin");
//					in.setInvoiceDate("20130514152245");
//					in.setPrintStatus(Short.parseShort("0"));
//					
//					InvoiceStub ins = new InvoiceStub();
//					ins.setInvoiceNo("00000099");
//					ins.setInvoiceCode("");
//					ins.setHeadCode("GTS_0000000028");
//					ins.setDrawer("superadmin");
//					ins.setInvoiceDate("20130514152256");
//					ins.setPrintStatus(Short.parseShort("0"));
//					
//					InvoiceStub inss = new InvoiceStub();
//					inss.setInvoiceNo("00000100");
//					inss.setInvoiceCode("");
//					inss.setHeadCode("GTS_0000000029");
//					inss.setDrawer("superadmin");
//					inss.setInvoiceDate("20130514152307");
//					inss.setPrintStatus(Short.parseShort("0"));
//					
//					stubs.add(in);
//					stubs.add(ins);
//					stubs.add(inss);


				}else{
					dealService.delectClientIp(Clientparam);//把打印成功的ip在表中删除
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.debug("{}", "data is exception");
		}


						
	}

  
}
